package com.wanlian.router.ui.me;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.smart.android.image.ImageLoader;
import com.wanlian.router.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Hyu on 2018/9/16.
 * Email: fvaryu@qq.com
 */
public class FeedbackImageAdapter extends RecyclerView.Adapter<FeedbackImageAdapter.H> {


    private List<String> photos;
    private OnItemClickListener mOnItemClickListener;

    public FeedbackImageAdapter() {
        photos = new ArrayList<>(5);
    }


    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener{
        void onItemClick(int position);
    }

    public int length() {
        int size = 0;
        for (String photo : photos) {
            if(!TextUtils.isEmpty(photo)){
                size ++;
            }
        }
        return size;
    }

    public void set(int position, String path) {
        photos.set(position, path);
        notifyDataSetChanged();
    }

    public void remove(int pos){
        photos.remove(pos);
        notifyDataSetChanged();
    }

    public void add(String path) {


        photos.add(path);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public H onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new H(LayoutInflater.from(viewGroup.getContext())
                .inflate(R.layout.layout_item_feedback, viewGroup, false)
        );
    }

    @Override
    public void onBindViewHolder(@NonNull H h, final int i) {
        if (i != photos.size() && !TextUtils.isEmpty(photos.get(i))) {
            String path = photos.get(i);
            ImageLoader.load(h.mImageView.getContext(), path, h.mImageView);
            h.mDelete.setVisibility(View.VISIBLE);
            h.mDesc.setVisibility(View.GONE);
        }else {
            h.mImageView.setImageResource(R.drawable.drawable_image_feedback);
            h.mDelete.setVisibility(View.GONE);
            h.mDesc.setVisibility(View.VISIBLE);
        }

        if (mOnItemClickListener != null) {
            View.OnClickListener clickListener = new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnItemClickListener.onItemClick(i);
                }
            };
            h.itemView.setOnClickListener(clickListener);
            h.mDelete.setOnClickListener(clickListener);
        }
    }

    @Override
    public int getItemCount() {
        return photos.size();
    }

    class H extends RecyclerView.ViewHolder {

        ImageView mImageView, mDelete;
        TextView mDesc;

        public H(@NonNull View itemView) {
            super(itemView);
            mImageView = itemView.findViewById(R.id.image_logo);
            mDelete = itemView.findViewById(R.id.image_delete);
            mDesc = itemView.findViewById(R.id.tv_desc);
        }
    }
}
